package tcu.iot.gasmanage.mapper.provider;

import tcu.iot.gasmanage.entity.equipment.EquipmentQuery;

public class EquipmentProvider {
    /*生成动态sql语句*/
    private void CreateSql(EquipmentQuery equipmentQuery, StringBuffer sql) {
        //通过判断传入的实体，相应字段是否为空，来进行sql语句的编写
        if (!equipmentQuery.getProvince().equals(""))
            sql.append("AND province= '").append(equipmentQuery.getProvince()).append("' ");
        if (!equipmentQuery.getRegion().equals(""))
            sql.append("AND region= '").append(equipmentQuery.getRegion()).append("' ");
        if (!equipmentQuery.getPipeline().equals(""))
            sql.append("AND pipeline= '").append(equipmentQuery.getPipeline()).append("' ");
        if (!equipmentQuery.getSite().equals(""))
            sql.append("AND site= '").append(equipmentQuery.getSite()).append("' ");
        if (!equipmentQuery.getEquipmentType().equals(""))
            sql.append("AND equipmentType= '").append(equipmentQuery.getEquipmentType()).append("' ");
        if (!equipmentQuery.getManagement().equals(""))
            sql.append("AND management= '").append(equipmentQuery.getManagement()).append("' ");
        if (!equipmentQuery.getPrincipal().equals(""))
            sql.append("AND principal= '").append(equipmentQuery.getPrincipal()).append("' ");
        if (!equipmentQuery.getEquipmentNumber().equals(""))
            sql.append("AND equipmentNumber= '").append(equipmentQuery.getEquipmentNumber()).append("' ");
    }

    public String SelectEquipment(EquipmentQuery equipmentQuery) {
        StringBuffer sql = new StringBuffer("SELECT * FROM equipment WHERE 1=1 ");
        CreateSql(equipmentQuery, sql);
        //ASC DESC分别代表按照id升序降序排列
        if (equipmentQuery.getSort().equals("+id"))
            sql.append("order by id ASC ");
        if (equipmentQuery.getSort().equals("-id"))
            sql.append("order by id DESC ");
        return sql.toString();
    }

    public String SelectEquipmentCount(EquipmentQuery equipmentQuery) {
        StringBuffer sql = new StringBuffer("SELECT count(*) FROM equipment WHERE 1=1 ");

        CreateSql(equipmentQuery, sql);

        return sql.toString();
    }

    public String FindEquipment(EquipmentQuery equipmentQuery) {
        StringBuffer sql = new StringBuffer("Select * FROM equipment WHERE 1=1 ");

        if (equipmentQuery.getSort().equals("+id"))
            sql.append("order by id ASC ");
        if (equipmentQuery.getSort().equals("-id"))
            sql.append("order by id DESC ");

        return sql.toString();
    }
}
